package com.xps.and.yuntong.Utils;

import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用特殊处理的单例拼接请求参数 并生成json对象
 */
public class RequestParams {

    private String type;
    private String content;
    private int uid;

    public JSONObject getJson() {
        StringBuilder sb = new StringBuilder();
        JSONObject json = new JSONObject();
        String jsonValue;

        sb.append("{");
        for (Field field : getClass().getDeclaredFields()) {

            if ("ourInstance".equals(field.getName() + "")
                    || "CommonRequest".equals(field.getName() + "")
                    || "serialVersionUID".equals(field.getName() + ""))
                continue;

            field.setAccessible(true);
            Object value = null;

            try {
                value = field.get(this);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            if (value != null && !TextUtils.isEmpty(value + "") && !"NULL".equals(value + ""))
                sb.append("\"" + field.getName() + "\"" + ":" + "\"" + value + "\"" + ",");

        }

        jsonValue = sb.toString();
        jsonValue = jsonValue.substring(0, jsonValue.length() - 1) + "}";
        try {
            json = new JSONObject(jsonValue);
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("RequestParams", "创建JSON异常");
            return json;
        }

        return json;
    }


    public static RequestParams getNewInstance() {
        return new RequestParams();
    }

    private RequestParams() {
        try {
            Reflect();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public void Reflect() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Field[] field = getClass().getDeclaredFields();
        for (int i = 0; i < field.length; i++) {
            String name = field[i].getName();
            name = name.substring(0, 1).toUpperCase() + name.substring(1);
            String type = field[i].getGenericType().toString();
            if (type.equals("class java.lang.String"))
                getClass().getDeclaredMethod("set" + name, String.class).invoke(this, "NULL");
        }
    }

    public String getType() {
        return type;
    }

    public RequestParams setType(String type) {
        this.type = type;
        return this;
    }

    public String getContent() {
        return content;
    }

    public RequestParams setContent(String content) {
        this.content = content;
        return this;
    }
    public int getUid() {
        return uid;
    }

    public RequestParams setUid(int uid) {
        this.uid = uid;
        return this;
    }

}
